import {
    autoCalcAmount,
    dividendType,
    queryParams,
    form,
    isCash,

} from './data'

import { listProduct_daily_netvalue } from "@/app/finace/api/finace/product_daily_netvalue";
import { formatDate } from "@vueuse/core";

function confirmDateChange(selectedDate: Date) {
    if (form.value.productId == null) return
    getPrice(selectedDate, form.value.productId)
}

function productIdChanged(productId: string) {
    if (form.value.confirmDate == null) return
    const dt = formatDate(form.value.confirmDate, 'YYYY-MM-DD')
    console.log(dt, { productId })
    getPrice(form.value.confirmDate, productId)
    // console.log(selectedDate)
}
function getPrice(selectedDate: Date, productId: string) {
    // const dt = formatDate(selectedDate,'YYYY-MM-DD')
    const dt = selectedDate
    // const dt=selectedDate.toISOString().replace('Z','').replace('.000',' ')
    const prodId = productId

    const param = { tradeDt: dt, productId: prodId }
    console.log(param)
    listProduct_daily_netvalue(param).then((res) => {
        console.log(res)
        // if(res.rows==null) return
        form.value.price = res.rows[0]?.unitNetValue
    })
}

function onPriceInput(input) {
    if (autoCalcAmount.value == false) return
    const value = input;
    // 这里可以添加额外的验证逻辑，例如限制数字范围
    // if (!/^\d*$/.test(value)) {
    //     input = value.slice(0, -1); // 移除最后一个非法字符
    // }
    if (input === '') return
    form.value.amount = parseFloat((form.value.quantity * form.value.price).toFixed(2))

}
function onQuantityInput(input) {
    if (autoCalcAmount.value == false) return
    if (input === '') return
    form.value.amount = parseFloat((form.value.quantity * form.value.price).toFixed(2))

}
function onAmountInput(input) {
    if (autoCalcAmount.value == false) return

    if (input === '') return

    form.value.quantity = isCash ? 0 : parseFloat(((form.value.amount - form.value.fee) / form.value.price).toFixed(2))
}

function onRowClicked(row) {
    console.log(row)
    queryParams.value.productId = row.productId

}

function onRadioChange(row) {
    console.log(row)
}


function onSoldOffSelectChange(val) {
    console.log(val)
}
function onTradeTypeChanged(type) {
    dividendType.value = null
    console.log(type)
}

export {
    onSoldOffSelectChange,
    onTradeTypeChanged,
    onRowClicked,
    onRadioChange,
    onPriceInput,
    onQuantityInput,
    onAmountInput,
    confirmDateChange,
    productIdChanged,
}